<?php

/**
 * @author Martin Pernica
 * @copyright 2009
 * Original based on MyDb class from Filip Pytloun
 */

class DbModel
{

    /**
     * Interface for SQL SELECT statement
     * 
     * @param string $from, array $where, string $other, string $num
     * @return array $return
     */
    public function select($from, $num = null, $where = null, $other = null)
    {

        if (is_null($num)) {
            $num = '*';
        }

        $temp = "SELECT " . $num . " FROM `{prefix}" . $from . "`";

        if (!is_null($where)) {
            if (is_array($where)) {
                $temp .= self::_where_helper($where);
            } else {
                throw new VException('Argument $where must be array !', 0, __file__, __line__);
            }
        }

        $query = Registry::get("db")->query($temp . " " . $other);

        $i = 0;
        $return = array();
        while ($data = $query->fetch_array()) {
            $return[$i] = $data;
            $i++;
        }
        return $return;
    }

    /**
     * Interface for SQL escape string
     * 
     * @param string $string
     * @return string $string
     */
    public function escape($string)
    {
        return Registry::get("db")->real_escape_string($string);
    }
    
    private static function _where_helper($where)
    {
        $temp .= " WHERE ";

                $max = count($where);
                $i = 1;
                foreach ($where as $key => $val) {

                    $and = " AND ";
                    if ($i == $max)
                        $and = " ";

                    if ($val{0} == "+") {
                        $val = substr($val, 1, strlen($val));
                        $temp .= $key . " = '" . $val . "'" . $and;
                    } elseif ($val{0} == "-") {
                        $val = substr($val, 1, strlen($val));
                        $temp .= $key . " != '" . $val . "'" . $and;
                    } elseif ($val{0} == "<") {
                        $val = substr($val, 1, strlen($val));
                        $temp .= $key . " < '" . $val . "'" . $and;
                    } elseif ($val{0} == ">") {
                        $val = substr($val, 1, strlen($val));
                        $temp .= $key . " > '" . $val . "'" . $and;
                    } elseif ($val{0} == "<=") {
                        $val = substr($val, 1, strlen($val));
                        $temp .= $key . " <= '" . $val . "'" . $and;
                    } elseif ($val{0} == ">=") {
                        $val = substr($val, 1, strlen($val));
                        $temp .= $key . " >= '" . $val . "'" . $and;
                    } else {
                        $temp .= $key . " = '" . $val . "'" . $and;
                    }
                    $i++;
                }
                
                return $temp;
    }

    /**
     * Interface for SQL INSERT statement
     * 
     * @param string $table, array $columns, array $values
     * @return bool
     */
    public function insert($table, $columns, $values)
    {
        $query = Registry::get("db")->query("INSERT INTO `{prefix}" . $table . "`(" .
            DbModel::getColumns($columns) . ") VALUES(" . DbModel::getValues($values) . ")");
        if ($query) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Interface for SQL DELETE statement
     * 
     * @param string $table, array $where, string $other
     * @return bool
     */
    public function delete($table, $where = null, $other = null)
    {
        $temp = "DELETE FROM `{prefix}" . $table . "`";

        if (!is_null($where)) {
            if (is_array($where)) {
                $temp .= self::_where_helper($where);
            } else {
                throw new VException('Argument $where must be array !', 0, __file__, __line__);
            }
        }

        $query = Registry::get("db")->query($temp . " " . $other);
        if ($query) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Interface for SQL UPDATE statement
     * 
     * @param string $table, array $set, array $where, string $other
     * @return bool
     */
    public function update($table, $set, $where = null, $other = null)
    {
        $temp = "UPDATE `{prefix}" . $table . "`";

        if (is_array($set)) {
            $temp .= " SET ";

            $max = count($set);
            $i = 1;
            foreach ($set as $key => $val) {

                $and = ", ";
                if ($i == $max)
                    $and = " ";

                $temp .= $key . " = '" . $val . "'" . $and;

                $i++;
            }
        } else {
            throw new VException('Argument $set must be array !', 0, __file__, __line__);
        }

        if (!is_null($where)) {
            if (is_array($where)) {
                $temp .= self::_where_helper($where);
            } else {
                throw new VException('Argument $where must be array !', 0, __file__, __line__);
            }
        }

        $query = Registry::get("db")->query($temp . " " . $other);

        if ($query) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Support function getValues
     * @param mixed $values
     * @return string
     */
    private function getValues($values)
    {
        if (is_array($values)) {
            $sql_commands = array('NOW()', 'NULL');
            $i = 0;
            foreach ($values as $key => $value) {
                if ($i == 0) {
                    if (in_array($value, $sql_commands)) {
                        $vals = DbModel::escape($value);
                        $i++;
                    } else {
                        $vals = "'" . DbModel::escape($value) . "'";
                        $i = 1;
                    }
                } else {
                    if (in_array($value, $sql_commands)) {
                        $vals = $vals . "," . DbModel::escape($value);
                    } else {
                        $vals = $vals . ",'" . DbModel::escape($value) . "'";
                    }
                }
            }
            return $vals;
        } else {
            throw new VException('Argument $values must be array !', 0, __file__, __line__);
            return 0;
        }

    }

    /**
     * Support function getColumns
     * @param mixed $columns
     * @return string
     */
    private function getColumns($columns)
    {
        if (is_array($columns)) {
            $i = 0;
            foreach ($columns as $key => $value) {
                if ($i == 0) {
                    $vals = "`" . DbModel::escape($value) . "`";
                    $i = 1;
                } else {
                    $vals = $vals . ",`" . DbModel::escape($value) . "`";
                }
            }
            return $vals;
        } else {
            throw new VException('Argument $columns must be array !', 0, __file__, __line__);
            return 0;
        }

    }

}


?>